<template>
	<view>
		<view class="fixed bg-white">
			<cu-custom :isBack="true">
				<block slot="backText"></block>
				<block slot="content">{{corpCode}}-货位盘点编辑</block>
			</cu-custom>
		</view>
		<view class="cu-bar bg-white search fixed indexes" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input @confirm="getData" v-model="inputVal" type="text" placeholder="输入关键词" focus></input>
			</view>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<scroll-view scroll-y="true" class="indexes"  :scroll-with-animation="true"
		 :enable-back-to-top="true" :style="[{height:'calc(100vh - '+ CustomBar + 'px)'}]">
			<view class="text-gray cu-form-group " style="color: red; margin-top: 13%;">
				<text>货位：{{posData.cposname}}</text>
			</view>
			<view class="text-gray cu-form-group " style="color: red;">
				<text>仓库：{{posData.cwhname}}</text>
			</view>
			<view class="cu-form-group" style="color: red;">
				<view class="title">检验：<text v-show="!checked">请扫描货位，检验货位是否正确</text></view>
				<checkbox class='round blue' :class="checked?'checked':''" :checked="checked?true:false"
				 value="1"></checkbox>
			</view>
			<view style="border: 1px #000000 solid"></view>
			<view class="bg-white margin-bottom" v-for="(item,index) in list" :key="index">
				<view class="text-gray cu-form-group">
					<text class="text-blue margin-right-xs">存货编码：{{item.viewCode}}</text> 
				</view >
				<view class="text-gray cu-form-group">
					<text class="text-blue margin-right-xs">存货名称：{{item.cinvname}}</text> 
				</view>
				<view class="text-gray cu-form-group">
					<text class="margin-right-xs" style="color: red;font-weight: 1000; font-size: 16px;">盘点数量：{{item.iquantity}}</text>
				</view>
				<view class="v-button">
					<button class="cu-btn tui-button-primary round bg-blue" @click="showModel(index,'+')">+</button>
					<button class="cu-btn tui-button-primary round bg-red" @click="showModel(index,'-')">-</button>
				</view>
			</view>
		</scroll-view>
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">数量</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					<view class="text-gray cu-form-group">
						<input placeholder="0" name="input" type="number" v-model="qty" focus="true"></input>
					</view>
				</view>
				<view class="cu-bar bg-white">
					<view class="action text-green" @tap="querModal">确定</view>
					<view class="action text-blue" @tap="hideModal">取消</view>
				</view>
			</view>
		</view>
		<view v-show="true">
			<audio id="audio" preload="auto" loop>
			  <source src="" type="audio/ogg" />
			</audio>
		</view>
		<view class="action-section">
			<button type="primary" class="no-border confirm-btn bg-red" :disabled="ishow" @click="createData()">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				checked:false,
				isCard:true,
				scrollTop:400,
				CustomBar: this.CustomBar, //滚动定位用
				inputVal:"",
				list:[],  //存货数据
				posData:{}, //货位数据
				hwcode:'', //货位编码
				taskid:'',//盘点任务单标志
				ishow:false,
				modalName:"",
				i : 0,
				qty : '',
				ys : '',
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'auditlist', 'corpCode']),
		},
		onLoad(data) {
			this.taskid = data.id;
			this.hwcode = data.code;
			this.loadData(data.code);
		},
		methods: {
			hideModal(e) {
				this.modalName = null;
				this.qty = '';
			},
			
			querModal(){
				this.modalName = null;
				if(this.ys == '+'){
					this.addQty(this.i);
				}else if(this.ys == '-'){
					this.subQty(this.i);
				}
			},
			
			showModel(index,ys){
				this.modalName = "Modal";
				this.i = index;
				this.ys = ys;
			},
			
			/** 加载执行*/
			loadData(cposcode){
				uni.request({
					url:this.serverURL_a+'/bas/basPosition/getPos',
					data:{
						cposcode : cposcode,
						__sid:this.userInfo.sessionid
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(res){
						if(res.statusCode!="200"){
							if(res.statusCode == "403"){
								uni.showModal({
								    content: "权限不足!",
									showCancel:false,
								});
								return;
							}
							uni.showModal({
							    content: res.message,
								showCancel:false,
							});
							return;
						}
						if(res.data.result=="false"){
							uni.showModal({
							  content: res.data.message,
							  showCancel:false,
							})
							return;
						}
						var pos = {
							cposcode:res.data.data.cposcode,
							cposname:res.data.data.cposname,
							cwhcode:res.data.data.basWarehouse.cwhcode,
							cwhname:res.data.data.basWarehouse.cwhname,
						}
						this.posData = pos;
					}.bind(this),
					fail: function() {
						uni.hideLoading()
						this.$api.msg('无法连接服务，请联系管理');
						this.ishow = false;
						return;
					}.bind(this)
				})
			},
			
			/** 扫码*/
			toScan(){
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						var rdata = res.result;
						this.inputVal = rdata;
						setTimeout(function(){
							this.getData();
						}.bind(this),100);
					}.bind(this)
				});
			},
			
			/** 获取或验证数据*/
			getData(){
				//1、盘点任务单获取完成后，扫描货位可直接跳转该货位明细
				if(this.inputVal.indexOf("hw") > -1){
					if(this.checked){
						this.$api.msg('该盘点货位已验证，请勿重新扫描!');
					}else{
						this.getPostData();
					}
					return;
				}else if(this.inputVal != '' 
					|| (this.inputVal.indexOf("sc") > -1 
					|| this.inputVal.indexOf("dh") > -1 
					|| this.inputVal.indexOf("inv") > -1 
					|| this.inputVal.indexOf("po") > -1 
					|| this.inputVal.indexOf("611-") > -1))
				{
					// || this.val.indexOf("inv") > -1 
					this.getInvData();
					return;
				}
				uni.showModal({
				    content: "请扫描正确的条码!",
					showCancel:false,
				});
				this.inputVal = '';
				return;
			},
			
			/** 获取货位信息*/
			getPostData(){
				uni.request({
					url:this.serverURL_a+'/so/App/getHw',
					data:{
						hw : this.inputVal,
						__sid:this.userInfo.sessionid
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(data){
						this.inputVal = '';
						var reslut = data.data;
						if(reslut.code == '1001'){
							var hwdata = reslut.resMap;
						}else{
							uni.showModal({
							    content: reslut.msg,
								showCancel:false,
							});
							return;
						}
						var resMap = reslut.resMap;
						if(this.posData.cposcode == resMap.cposcode && this.posData.cwhcode == resMap.basWarehouse.cwhcode){
							this.checked = true;
							this.$api.msg('验证成功!');
						}else{
							this.$api.msg('验证失败!');
						}
					}.bind(this),
					fail:function(){
						this.$api.msg('服务已停止,请稍后再试');
						return;
					}.bind(this),
				});
			},
			
			/** 获取存货*/
			getInvData(){
				uni.request({
					// url:this.serverURL_a+'/bas/basInventory/findMapCinvcode',
					url:this.serverURL_a+'/barcode/decode/getInvInfo',
					data:{
						// hw : this.inputVal,
						__sid:this.userInfo.sessionid,
						barCode: this.inputVal,
						bneedStore: '0',
						bmustAcct:'0'
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(res){
						this.inputVal = '';
						if(res.statusCode!="200"){
							if(res.statusCode == "403"){
								uni.showModal({
								    content: "权限不足!",
									showCancel:false,
								});
								return;
							}
							uni.showModal({
							    content: data.message,
								showCancel:false,
							});
							return;
						}
						if(res.data.result=="false"){
							uni.showModal({
							  content: res.data.message,
							  showCancel:false,
							})
							console.log("=====testClick=====");
							let src = '../../static/smsb.mp3';
							//实例化声音  
							const Audio = uni.createInnerAudioContext();
							Audio.autoplay = true;
							Audio.src = src; //音频地址  
							Audio.play(); //执行播放  
							Audio.onError((res) => {
								console.log(res.errMsg);
								console.log(res.errCode);
							});
							Audio.onPause(function() {
								console.log('end');
								Audio.destroy();
							});
							return;
						}else{
							this.getInvList(res.data);
							console.log("=====testClick=====");
							let src = '../../static/smcg.mp3';
							//实例化声音  
							const Audio = uni.createInnerAudioContext();
							Audio.autoplay = true;
							Audio.src = src; //音频地址  
							Audio.play(); //执行播放  
							Audio.onError((res) => {
								console.log(res.errMsg);
								console.log(res.errCode);
							});
							Audio.onPause(function() {
								console.log('end');
								Audio.destroy();
							});
						}
					}.bind(this),
					fail: function() {
						uni.hideLoading()
						this.$api.msg('无法连接服务，请联系管理');
						this.ishow = false;
						return;
					}.bind(this)
				})
			},
			
			/** 给存货list赋值*/
			getInvList(data){
				let inventory = data.data.basInv;
				let bool = true;
				for(var i = 0;i<this.list.length;i++){
					if(this.list[i].cinvcode == inventory.cinvcode){
						this.list[i].iquantity =  Number(this.list[i].iquantity) + 1;
						bool = false;
						break;
					}
					bool = true;
				}
				if(bool){
					var inv = {
						cinvcode : inventory.cinvcode,
						cinvname : inventory.cinvname,
						viewCode : inventory.viewCode,
						iquantity : 1,
						qty:'',
					}
					this.list.push(inv);
				}
			},
			
			/** 加*/
			addQty(index){
				if(this.qty == 0 || this.qty == '' || this.qty == undefined){
					return;	
				}
				this.list[index].iquantity = Number(this.list[index].iquantity) + Number(this.qty);
				this.qty = '';
			},
			
			/** 减*/
			subQty(index){
				if(this.qty == 0 || this.qty == '' || this.qty == undefined){
					return;	
				}
				var qty = Number(this.list[index].iquantity) - Number(this.qty);
				if(qty < 0){
					this.qty = '',
					uni.showModal({
					    content: '减掉后数量为负,请重新修改!',
						showCancel:false,
					});
					return;	
				}
				this.list[index].iquantity = qty;
				this.qty = '';
			},
			
			/** 提交数据*/
			createData(){
				this.ishow = true;
				if(this.taskid == undefined || this.taskid == '' || this.hwcode == undefined || this.hwcode == ''){
					this.$api.msg("请检查数据，货位信息不存在!");
					this.ishow = false;
					return;
				}
				if(!this.checked){
					this.$api.msg("货位未验证，请验证!");
					this.ishow = false;
					return;
				}
				if(this.list == undefined || this.list.length <= 0){
					this.$api.msg("请检查数据，存货信息不存在!");
					this.ishow = false;
					return;
				}
				
				var invs = JSON.stringify(this.list);
				//提交数据
				uni.showModal({
					content: '是否确认提交',
					success: function(res) {
						if(res.confirm){
							uni.showLoading();
							this.saveData(this.taskid,this.hwcode,invs);
						}else{
							this.ishow = false;
						}
					}.bind(this)
				});
			},
			
			/** 提交数据*/
			saveData(taskid,cposcode,invs){
				uni.request({
					url:this.serverURL_a+'/wh/check/api/whCheckinvTask/saveCheckTaskRds',
					data:{
						taskId:taskid,
						cposcode : cposcode,
						invs:invs,
						__sid:this.userInfo.sessionid
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(res){
						uni.hideLoading();
						if(res.statusCode!="200"){
							uni.showModal({
								content: res.message,
								showCancel:false,
							});
							this.ishow = false;
							return;
						}
						if(res.data.result=="false"){
							uni.showModal({
							  content: res.data.message,
							  showCancel:false,
							})
							this.ishow = false;
							return;
						}
						this.list = [];
						this.$api.msg('提交成功!');
						this.ishow = false;
						return;
					}.bind(this),
					fail: function() {
						uni.hideLoading();
						this.$api.msg('无法连接服务，请联系管理');
						this.ishow = false;
						return;
					}.bind(this)
				})
			},
		}
	}
</script>

<style>
.v-button{
		margin-bottom: 5px;
		display: block;
		height: 30px;
		width: 100%;
	}
.tui-button-primary {
		width: 85px;
		height: 27px;
		font-size: 14px;
		float: left;
		line-height: 27px;
		margin-right: 5px;
	}
	
/* 底部栏 */
.action-section{
	/* #ifdef H5 */
	/* #endif */
	position:fixed;
	left: 75%;
	bottom:30upx;
	z-index: 95;
	display: flex;
	align-items: center;
	background: rgba(255,255,255,.9);
	border-radius: 40upx;
	.checkbox{
		height:52upx;
		position:relative;
		image{
			width: 52upx;
			height: 100%;
			position:relative;
			z-index: 5;
		}
	}
	.clear-btn{
		position:absolute;
		left: 26upx;
		top: 0;
		z-index: 4;
		width: 0;
		height: 52upx;
		line-height: 52upx;
		padding-left: 38upx;
		font-size: $font-base;
		color: #fff;
		background: $font-color-disabled;
		border-radius:0 50px 50px 0;
		opacity: 0;
		transition: .2s;
		&.show{
			opacity: 1;
			width: 120upx;
		}
	}
	.total-box{
		flex: 1;
		display:flex;
		flex-direction: column;
		text-align:right;
		padding-right: 40upx;
		.price{
			font-size: $font-lg;
			color: $font-color-dark;
		}
		.coupon{
			font-size: $font-sm;
			color: $font-color-light;
			text{
				color: $font-color-dark;
			}
		}
	}
	.confirm-btn{
		padding: 0 38upx;
		margin: 0;
		border-radius: 100px;
		height: 76upx;
		line-height: 76upx;
		font-size: $font-base + 2upx;
		background: $uni-color-primary;
	}
}
</style>
